﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Animation;
using MapLibrary.Utility;

namespace MapLibrary.Players.Abilities
{
    class Sliding : Ability
    {
        private const int reuseTime = 60;
        private const float MAX_VEL_X = 500;

        private int cooldown;
        private Player player;
        private Vector2 velocity;
        private bool active;
        private TimerUtility timer; 

        public Sliding(Player p)
        {
            player = p;
            cooldown = reuseTime;
            velocity = new Vector2(100.0f, 0.0f);

            timer = new TimerUtility(Deactivate,20);
            timer.Active = false;
            active = false;
        }

        public override void  Update()
        {
            cooldown--;
            if(cooldown < 0){
                cooldown = 0;
            }
            timer.Update();

 	    }

        public override bool  ActivateConditionsSatisfied(Player p)
        {
 	        return cooldown <= 0 && Math.Abs(p.Body.LinearVelocity.Y) < 0.01f;
        }


        public override void  Activate(Player p)
        {
            String state = player.Type.CurrentState;
            if (state.Equals("Running Right") || state.Equals("Idle Right"))
            {
                p.Body.ApplyLinearImpulse(velocity);
                p.Type.CurrentState = "Sliding Right";
            }
            else
            {
                p.Body.ApplyLinearImpulse(-velocity);
                p.Type.CurrentState = "Sliding Left";
            }
            player.Controller.MaxLinearVelocityX = MAX_VEL_X;
            timer.Active = true;
            cooldown = reuseTime;
        }

        public void Deactivate()
        {
            player.Controller.MaxLinearVelocityX = player.MAX_VELOCITY_X;
            timer.Active = false;
            active = false;

            float velx = player.Body.LinearVelocity.X;
            if (velx > 0)
            {
                player.Type.CurrentState = "Running Right";
            }
            else if (velx < 0)
            {
                player.Type.CurrentState = "Running Left";
            }
        }
    }
}
